import axios from 'axios'
import {
  Message
} from 'element-ui'
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000 // request timeout
})

service.interceptors.request.use(
  // config => {
  //   let token = util.getToken()
  //   if (token) {
  //     config.headers.common['Authorization'] = 'Bearer ' + token;
  //   } else {
  //     config.headers.common['Authorization'] = 'Basic ' + 'YXBwOnNlY3JldA==';
  //   }
  //   return config
  // },
  // error => {
  //   return Promise.reject(error)
  // }
)

// response interceptor
service.interceptors.response.use(response => {
  if (response.status == 200) {
    // if (response.headers['content-type'].toLowerCase() != 'application/json;charset=utf-8') {
    //   return response.data;
    // }
    if (response.data.code == '000000') {
      return Promise.resolve(response.data);
    } else if (response.data.token_type) {
      return Promise.resolve(response.data);
    } else {
      Message({
        message: response.data.msg,
        type: "error"
      });
      return Promise.reject(response.data);
    }
  } else {
    Message({
      message: "网络错误!",
      type: "error"
    });
  }
  // return Promise.reject(response.data);
}, error => {
  console.log(error);
  Message({
    message: error.response.data.msg?error.response.data.msg:'系统错误',
    type: "error"
  });
  return Promise.reject(error.response);
});

export default service